﻿Ext.define("ProjectPhase_ProjectPhaseModel", {
    extend: "Ext.data.Model",
    fields: []
});

Ext.define('app.prm.project.ProjectPhase', {
    extend: 'Ext.grid.Panel',
    border: false,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    forceFit: false,
    enableLocking: true,
    columns: [],
    columnLines: true,
    controller: 'project',
    requires: ['app.prm.project.ProjectController'],
    initComponent: function () {
        var me = this;

      

        me.tbar = [
            {
                text: '已完成',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseFinsh'
            },
            {
                text: '未完成',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseUnFinsh'
            },
            {
                text: 'N/A',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseUnKown'
            },
            {
                text: '刷新',
                glyph: 0xf021,
                handler: function () {
                    me.loadData();
                }
            },
            '->',
            {
                checked: true,
                boxLabel: "阶段 | 项目",
                ref: 'phase_top_cheched',
                xtype: 'checkbox', listeners: { change: function () { me.radiochange() } }
            },
            '-',
            {
                xtype: 'radiogroup', listeners: {
                    change: function () { me.radiochange() }
                },
                defaults: {
                    width: 150
                },
                items: [
                {
                    xtype: 'radio', checked: true, name: 'summaryType',
                    boxLabel: '项目阶段进度明细', inputValue: 'project-phase-detial'
                },
            {
                xtype: 'radio', name: 'summaryType', width: 110,
                boxLabel: '项目阶段进度', inputValue: 'project-phase'
            }
            ,
            {
                xtype: 'radio', name: 'summaryType',
                boxLabel: '产品阶段进度明细', inputValue: 'product-phase-detial'
            },
            {
                xtype: 'radio', name: 'summaryType',
                boxLabel: '产品阶段进度', inputValue: 'product-phase'
            }
                ]
            }


        ];
        me.callParent();
    },
    listeners: {
        afterrender: function () {
            this.loadData();
        },
        celldblclick: 'onCellClick'
    },
    loadData: function () {
        this.loadmask = new Ext.LoadMask(this, { msg: "loading" });
        this.loadmask.show();
        var main = this;
        main.requestDataNum = 0;
        App.AjaxGet('/api/services/app/phaseInfo/GetPhaseInfo', function (data) {
            //loadPhase
            main.phaseItems = data.result;
            main.dataReady();
        });
        App.AjaxGet('/api/services/app/project/GetProjectPhaseReport', function (data) {
            //loadProduct
            main.projectPhaseItems = data.result;
            main.dataReady();
        });
    },
    dataReady: function () {

        this.requestDataNum = this.requestDataNum + 1;
        if (this.requestDataNum > 1) {
            this.loadmask.hide();
            this.project_phase_detial();
        }

    },
    radiochange: function () {

        var summaryType = this.query('radiogroup')[0].getValue().summaryType;
        if (summaryType == "project-phase-detial") {
            this.project_phase_detial();
        } else if (summaryType == "project-phase") {
            this.project_phase();
        } else if (summaryType == "product-phase-detial") {
            this.product_phase_detial();
        } else if (summaryType == "product-phase") {
            this.product_phase();
        }


    },
    project_phase_detial: function () {
        var main = this;
        var cc = this.query('checkbox')[0].checked;
        if (cc) {
            main.createProjectPhaseColumnFields(true);
            main.loadProjectPhaseStore();
        } else {
            main.createPhaseProjectColumnFields(true);
            main.loadPhaseProjectStore(true);
        }

    },

    project_phase: function () {
        var main = this;
        var cc = this.query('checkbox')[0].checked;
        if (cc) {
            main.createProjectPhaseColumnFields(false);
            main.loadProjectPhaseStore();
        } else {
            main.createPhaseProjectColumnFields(false);
            main.loadPhaseProjectStore(false);
        }
    },


    createProjectPhaseColumnFields: function (isPhaseDetial) {
        var main = this;
        var columns = [];
        var fields = [];
        columns.push({ header: '客户', dataIndex: 'customer', locked: true });
        columns.push({ header: '项目名称', dataIndex: 'projectName', locked: true });
        columns.push({
            header: '项目编号', dataIndex: 'projectNo', locked: true, renderer:
                function (val) {
                    return '<span style="color:blue">' + val + '</span>'
                }
        });
        columns.push({
            header: '产品号', dataIndex: 'productNo', locked: true, renderer:
                    function (val) {
                        return '<span style="color:blue">' + val + '</span>'
                    }
        });
        //customer
        fields.push('customer');
        fields.push('projectId');
        fields.push('productId');
        fields.push('projectName');
        fields.push('projectNo');
        fields.push('productNo');

        var phaseItems = main.phaseItems;
        for (var i = 0; i < phaseItems.length; i++) {
            var record = phaseItems[i];

            var obj = {
                header: record.name, dataIndex: record.name, renderer: main.renderPhase
            };
            fields.push(record.name);
            fields.push(record.name + "IsFinsh");
            if (isPhaseDetial && record.phaseItems.length > 0) {
                var firstColumn = { header: record.name, columns: [] };
                firstColumn.columns.push(obj);
                for (var j = 0; j < record.phaseItems.length; j++) {
                    var child = record.phaseItems[j];
                    firstColumn.columns.push({
                        header: child.name, dataIndex: child.name, renderer: main.renderPhase
                    });
                    fields.push(child.name);
                    fields.push(child.name + "IsFinsh");
                }
                columns.push(firstColumn);
            }
            else {
                columns.push(obj);
            }
        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadProjectPhaseStore: function () {
        var summryFields = this.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        ProjectPhase_ProjectPhaseModel.removeFields(ProjectPhase_ProjectPhaseModel.fields);
        ProjectPhase_ProjectPhaseModel.addFields(summryFields);

        var projectPhaseItems = this.projectPhaseItems;
        for (var i = 0; i < projectPhaseItems.length; i++) {
            var data = projectPhaseItems[i];
            var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
            });

            for (var j = 0; j < summryFields.length; j++) {
                if (summryFields[j].indexOf("IsFinsh") > 0) {
                    if (data[summryFields[j]] != undefined) {
                        p.set(summryFields[j], data[summryFields[j]] ? 1 : 0);
                    } else {
                        p.set(summryFields[j], -1);
                    }

                } else {
                    if (data[summryFields[j]]) {
                        p.set(summryFields[j], data[summryFields[j]]);
                    }
                }

            }
            store.add(p);
        }

        store.commitChanges();
        this.reconfigure(store, this.summryColumns);
    },

    createPhaseProjectColumnFields: function (isPhaseDetial) {
        var main = this;
        var columns = [];
        var fields = [];
        if (isPhaseDetial) {
            columns.push({
                header: 'PhaseL1', dataIndex: 'PhaseL1', locked: true, renderer: function (value, metaData, record, rowIndex) {
                    if (rowIndex > 0) {
                        if (value == record.store.getAt(rowIndex - 1).get('PhaseL1')) {
                            return "";
                        }
                    }

                    return value;
                }
            });
            fields.push('PhaseL1');
            columns.push({
                header: 'PhaseL2', dataIndex: 'PhaseL2', locked: true, renderer: function (value, metaData, record, rowIndex) {
                    if (value == record.get('PhaseL1')) {
                        return '';
                    }
                    return value;
                }
            });
            fields.push('PhaseL2');
        }
        else {

            columns.push({
                header: 'Phase', locked: true, dataIndex: 'PhaseL2'
            });
            fields.push('PhaseL2');
        }

        var projectPhaseItems = main.projectPhaseItems;
        for (var i = 0; i < projectPhaseItems.length; i++) {
            var record = projectPhaseItems[i];

            var obj = {
                header: record.projectNo, dataIndex: 'pro' + record.projectId, renderer: main.renderPhase
            };
            fields.push('pro' + record.projectId);
            fields.push('pro' + record.projectId + "IsFinsh");
            columns.push(obj);

        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadPhaseProjectStore: function (isPhaseDetial) {
        var main = this;
        var summryFields = main.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        ProjectPhase_ProjectPhaseModel.removeFields(ProjectPhase_ProjectPhaseModel.fields);
        ProjectPhase_ProjectPhaseModel.addFields(summryFields);

        var phaseItems = main.phaseItems;
        for (var i = 0; i < phaseItems.length; i++) {
            var record = phaseItems[i];
            if (isPhaseDetial && record.phaseItems.length > 0) {
                var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
                    'PhaseL1': record.name,
                    'PhaseL2': record.name,
                });

                store.add(p);
                for (var j = 0; j < record.phaseItems.length; j++) {
                    var child = record.phaseItems[j];
                    var pItem = Ext.create("ProjectPhase_ProjectPhaseModel", {
                        'PhaseL1': record.name,
                        'PhaseL2': child.name,
                    });
                    store.add(pItem);
                }
            }
            else {
                var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
                    'PhaseL1': record.name,
                    'PhaseL2': record.name,
                });
                store.add(p);
            }
        }
        main.loadPhaseProjectRecord(store);

        store.commitChanges();
        main.reconfigure(store, this.summryColumns);
    },
    loadPhaseProjectRecord: function (store) {
        var main = this;
        var projectPhaseItems = main.projectPhaseItems;
        store.each(function (r) {
            var phase = r.get("PhaseL2");
            for (var i = 0; i < projectPhaseItems.length; i++) {
                var p = projectPhaseItems[i];
                //'pro' + record.projectId + "IsFinsh"
                var propety1 = phase;
                var propety2 = phase + "IsFinsh";
                if (p[propety1]) {
                    r.set('pro' + p['projectId'], p[propety1]);
                }
                if (p[propety2] != undefined) {
                    r.set('pro' + p['projectId'] + "IsFinsh", p[propety2] ? 1 : 0);
                }
                else {
                    r.set('pro' + p['projectId'] + "IsFinsh", -1);
                }
            }
        });
    },

    product_phase_detial: function () {
        var main = this;
        var cc = this.query('checkbox')[0].checked;
        if (cc) {
            main.createProductPhaseColumnFields(true);
            main.loadProductPhaseStore();
        } else {
            main.createPhaseProductColumnFields(true);
            main.loadPhaseProductStore(true);
        }
    },
    product_phase: function () {
        var main = this;
        var cc = this.query('checkbox')[0].checked;
        if (cc) {
            main.createProductPhaseColumnFields(false);
            main.loadProductPhaseStore();
        } else {
            main.createPhaseProductColumnFields(false);
            main.loadPhaseProductStore(false);
        }
    },
    createProductPhaseColumnFields: function (isPhaseDetial) {
        var main = this;
        var columns = [];
        var fields = [];
        columns.push({ header: '产品号', locked: true, dataIndex: 'productNo' });
        fields.push('productNo');
        fields.push('productId');

        var phaseItems = main.phaseItems;
        for (var i = 0; i < phaseItems.length; i++) {
            var record = phaseItems[i];

            var obj = {
                header: record.name, dataIndex: record.name, renderer: main.renderPhase
            };
            fields.push(record.name);
            fields.push(record.name + "IsFinsh");
            fields.push(record.name + "Dt");
            if (isPhaseDetial && record.phaseItems.length > 0) {
                var firstColumn = { header: record.name, columns: [] };
                firstColumn.columns.push(obj);
                for (var j = 0; j < record.phaseItems.length; j++) {
                    var child = record.phaseItems[j];
                    firstColumn.columns.push({
                        header: child.name, dataIndex: child.name, renderer: main.renderPhase
                    });
                    fields.push(child.name);
                    fields.push(child.name + "Dt");
                    fields.push(child.name + "IsFinsh");
                }
                columns.push(firstColumn);
            }
            else {
                columns.push(obj);
            }
        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadProductPhaseStore: function () {
        var summryFields = this.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });
        var dtEmpty = new Date(1900, 1, 1);
        ProjectPhase_ProjectPhaseModel.removeFields(ProjectPhase_ProjectPhaseModel.fields);
        ProjectPhase_ProjectPhaseModel.addFields(summryFields);

        var projectPhaseItems = this.projectPhaseItems;
        for (var i = 0; i < projectPhaseItems.length; i++) {
            var data = projectPhaseItems[i];
            var product = data["productNo"];
            var dtp = new Date(data["projectStart"]);
            if (product && product != "") {
                var rindex = store.findBy(function (r) {
                    return r.get("productNo") == product;
                });
                if (rindex < 0) {
                    var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
                        "productNo": product
                    });


                    for (var j = 0; j < summryFields.length; j++) {
                        if (summryFields[j].indexOf("Dt") == -1 && summryFields[j].indexOf("IsFinsh") == -1 && summryFields[j].indexOf("productNo") == -1) {
                            if (data[summryFields[j]] != undefined) {
                                p.set(summryFields[j], data[summryFields[j]]);
                                p.set(summryFields[j] + "Dt", dtp);
                                p.set(summryFields[j] + "IsFinsh", data[summryFields[j] + "IsFinsh"] ? 1 : 0);
                            } else {
                                p.set(summryFields[j] + "IsFinsh", -1);
                                p.set(summryFields[j] + "Dt", dtEmpty);
                            }
                        }


                    }
                    store.add(p);
                } else {
                    p = store.getAt(rindex);
                    for (var j = 0; j < summryFields.length; j++) {
                        if (summryFields[j].indexOf("Dt") == -1 && summryFields[j].indexOf("IsFinsh") == -1 && summryFields[j].indexOf("productNo") == -1) {
                            if (data[summryFields[j]] != undefined && p.get(summryFields[j] + "Dt") < dtp) {
                                p.set(summryFields[j], data[summryFields[j]]);
                                p.set(summryFields[j] + "Dt", dtp);
                                p.set(summryFields[j] + "IsFinsh", data[summryFields[j] + "IsFinsh"] ? 1 : 0);
                            }
                        }
                    }
                }

            }

        }

        store.commitChanges();
        this.reconfigure(store, this.summryColumns);
    },

    createPhaseProductColumnFields: function (isPhaseDetial) {
        var main = this;
        var columns = [];
        var fields = [];
        if (isPhaseDetial) {
            columns.push({
                header: 'PhaseL1', locked: true, dataIndex: 'PhaseL1', renderer: function (value, metaData, record, rowIndex) {
                    if (rowIndex > 0) {
                        if (value == record.store.getAt(rowIndex - 1).get('PhaseL1')) {
                            return "";
                        }
                    }

                    return value;
                }
            });
            fields.push('PhaseL1');
            columns.push({
                header: 'PhaseL2', locked: true, dataIndex: 'PhaseL2', renderer: function (value, metaData, record, rowIndex) {
                    if (value == record.get('PhaseL1')) {
                        return '';
                    }
                    return value;
                }
            });
            fields.push('PhaseL2');
        }
        else {

            columns.push({
                header: 'Phase', locked: true, dataIndex: 'PhaseL2'
            });
            fields.push('PhaseL2');
        }

        var projectPhaseItems = main.projectPhaseItems;
        for (var i = 0; i < projectPhaseItems.length; i++) {
            var record = projectPhaseItems[i];
            var product = record.productNo;
            if (product && product != '') {
                var obj = {
                    header: product, locked: true, dataIndex: 'product' + product, renderer: main.renderPhase
                };

                var colExist = false;
                Ext.each(columns, function (c) {

                    if (c.header == product) {
                        colExist = true;
                    }
                });
                if (!colExist) {
                    fields.push('product' + product);
                    fields.push('product' + product + "IsFinsh");
                    fields.push('product' + product + "Dt");
                    columns.push(obj);
                }
            }
        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadPhaseProductStore: function (isPhaseDetial) {
        var main = this;
        var summryFields = main.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        ProjectPhase_ProjectPhaseModel.removeFields(ProjectPhase_ProjectPhaseModel.fields);
        ProjectPhase_ProjectPhaseModel.addFields(summryFields);

        var phaseItems = main.phaseItems;
        for (var i = 0; i < phaseItems.length; i++) {
            var record = phaseItems[i];
            if (isPhaseDetial && record.phaseItems.length > 0) {
                var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
                    'PhaseL1': record.name,
                    'PhaseL2': record.name,
                });

                store.add(p);
                for (var j = 0; j < record.phaseItems.length; j++) {
                    var child = record.phaseItems[j];
                    var pItem = Ext.create("ProjectPhase_ProjectPhaseModel", {
                        'PhaseL1': record.name,
                        'PhaseL2': child.name,
                    });
                    store.add(pItem);
                }
            }
            else {
                var p = Ext.create("ProjectPhase_ProjectPhaseModel", {
                    'PhaseL1': record.name,
                    'PhaseL2': record.name,
                });
                store.add(p);
            }
        }
        main.loadPhaseProductRecord(store);

        store.commitChanges();
        main.reconfigure(store, this.summryColumns);
    },
    loadPhaseProductRecord: function (store) {
        var main = this;
        var summryFields = main.summryFields;
        var dtEmpty = new Date(1900, 1, 1);
        store.each(function (r) {
            for (var j = 0; j < summryFields.length; j++) {
                if (summryFields[j].indexOf("Dt") == -1 && summryFields[j].indexOf("IsFinsh") == -1 && summryFields[j].indexOf("Phase") == -1) {
                    r.set(summryFields[j] + "Dt", dtEmpty);
                    r.set(summryFields[j] + "IsFinsh", -1);
                }
            }
        });


        var projectPhaseItems = main.projectPhaseItems;
        store.each(function (r) {
            var phase = r.get("PhaseL2");
            for (var i = 0; i < projectPhaseItems.length; i++) {
                var p = projectPhaseItems[i];
                var product = p["productNo"];
                var dtp = new Date(p["projectStart"]);

                var propety1 = phase;
                var propety2 = phase + "IsFinsh";



                //1\有这个属性，且时间明显新鲜于当前，才确认覆盖
                //
                if (p[propety1] && dtp > r.get('product' + product + "Dt")) {
                    r.set('product' + product, p[propety1]);
                    if (p[propety2] != undefined) {
                        r.set('product' + product + "IsFinsh", p[propety2] ? 1 : 0);
                    }
                }

            }
        });
    },

    renderPhase: function (value, p, _record) {

        if (!value) {
            value = '';
        }
        var isFinish = _record.data[p.column.dataIndex + "IsFinsh"];
        if (isFinish >= 0) {
            p.tdCls = isFinish > 0 ? "phaseFinsh" : "phaseUnFinsh";
            p.tdAttr = 'data-qtip="' + value + '"';
        }
        else {
            p.tdCls = "phaseUnKown";
        }
        return value;
    }
});